<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
{
    public function up(): void
    {
        if (! Schema::hasColumn('sub_settlements', 'tax')) {
            Schema::table('sub_settlements', function (Blueprint $table) {
                $table->decimal('tax', 15, 2)->default(0)->comment('个税金额')->after('service_fee_rate');
            });
        }

        if (! Schema::hasColumn('sub_settlements', 'required_balance')) {
            Schema::table('sub_settlements', function (Blueprint $table) {
                $table->decimal('required_balance', 15, 2)->default(0)->comment('所需余额')->after('tax');
            });
        }

        if (! Schema::hasColumn('settlements', 'total_required_balance')) {
            Schema::table('settlements', function (Blueprint $table) {
                $table->decimal('total_required_balance', 15, 2)->default(0)->comment('总所需余额')->after('total_amount');
            });
        }

        $mapping = [
            'pending_verify'     => '待四要素验证',
            'verifying'          => '四要素验证中',
            'verify_failed'      => '四要素验证失败',
            'pending_tax'        => '待算税',
            'pending_submit'     => '待提交',
            'pending_distribute' => '待发放',
            'distributing'       => '发放中',
            'completed'          => '已完成',
            'cancelled'          => '已取消',
            'distribute_failed'  => '发放失败',
            'partial_failed'     => '部分失败',
            'refunded'           => '已退款',
        ];

        DB::statement("ALTER TABLE settlements MODIFY COLUMN status VARCHAR(32) NOT NULL DEFAULT '待四要素验证' COMMENT '结算状态'");

        foreach ($mapping as $from => $to) {
            DB::table('settlements')->where('status', $from)->update(['status' => $to]);
        }

        $enumValues = implode("','", array_values($mapping));
        DB::statement("ALTER TABLE settlements MODIFY COLUMN status ENUM('{$enumValues}') NOT NULL DEFAULT '待四要素验证' COMMENT '结算状态'");

        $subMapping = [
            'pending_verify'     => '待验证',
            'verifying'          => '验证中',
            'pending_tax'        => '待算税',
            'pending_submit'     => '待提交',
            'pending_distribute' => '待发放',
            'distributing'       => '发放中',
            'completed'          => '已完成',
            'cancelled'          => '已取消',
            'distribute_failed'  => '发放失败',
            'verify_failed'      => '验证失败',
            'refunded'           => '已退票',
        ];

        DB::statement("ALTER TABLE sub_settlements MODIFY COLUMN status VARCHAR(32) NOT NULL DEFAULT '验证中' COMMENT '状态'");

        foreach ($subMapping as $from => $to) {
            DB::table('sub_settlements')->where('status', $from)->update(['status' => $to]);
        }

        $subEnumValues = implode("','", array_values($subMapping));
        DB::statement("ALTER TABLE sub_settlements MODIFY COLUMN status ENUM('{$subEnumValues}') NOT NULL DEFAULT '验证中' COMMENT '状态'");
    }

    public function down(): void
    {
        if (Schema::hasColumn('sub_settlements', 'required_balance')) {
            Schema::table('sub_settlements', function (Blueprint $table) {
                $table->dropColumn('required_balance');
            });
        }

        if (Schema::hasColumn('sub_settlements', 'tax')) {
            Schema::table('sub_settlements', function (Blueprint $table) {
                $table->dropColumn('tax');
            });
        }

        if (Schema::hasColumn('settlements', 'total_required_balance')) {
            Schema::table('settlements', function (Blueprint $table) {
                $table->dropColumn('total_required_balance');
            });
        }

        $reverseMapping = [
            '待四要素验证' => 'pending_verify',
            '四要素验证中' => 'verifying',
            '四要素验证失败' => 'verify_failed',
            '待算税' => 'pending_tax',
            '待提交' => 'pending_submit',
            '待发放' => 'pending_distribute',
            '发放中' => 'distributing',
            '已完成' => 'completed',
            '已取消' => 'cancelled',
            '发放失败' => 'distribute_failed',
            '部分失败' => 'partial_failed',
            '已退款' => 'refunded',
        ];

        DB::statement("ALTER TABLE settlements MODIFY COLUMN status VARCHAR(32) NOT NULL DEFAULT 'pending_submit' COMMENT '结算状态'");

        foreach ($reverseMapping as $from => $to) {
            DB::table('settlements')->where('status', $from)->update(['status' => $to]);
        }

        $enumValues = implode("','", array_values($reverseMapping));
        DB::statement("ALTER TABLE settlements MODIFY COLUMN status ENUM('{$enumValues}') NOT NULL DEFAULT 'pending_submit' COMMENT '结算状态'");

        $subReverseMapping = [
            '待验证' => 'pending_verify',
            '验证中' => 'verifying',
            '待算税' => 'pending_tax',
            '待提交' => 'pending_submit',
            '待发放' => 'pending_distribute',
            '发放中' => 'distributing',
            '已完成' => 'completed',
            '已取消' => 'cancelled',
            '发放失败' => 'distribute_failed',
            '验证失败' => 'verify_failed',
            '已退票' => 'refunded',
        ];

        DB::statement("ALTER TABLE sub_settlements MODIFY COLUMN status VARCHAR(32) NOT NULL DEFAULT 'verifying' COMMENT '状态'");

        foreach ($subReverseMapping as $from => $to) {
            DB::table('sub_settlements')->where('status', $from)->update(['status' => $to]);
        }

        $subEnumValues = implode("','", array_values($subReverseMapping));
        DB::statement("ALTER TABLE sub_settlements MODIFY COLUMN status ENUM('{$subEnumValues}') NOT NULL DEFAULT 'verifying' COMMENT '状态'");
    }
};
